// 集中管理跟产品逻辑相关的异步请求
import request from "@/utils/request";

export const bannerGet = () => {
  return request.get("classes/VueBanner");
};
//更新banner
export const bannerPut = (bannerid, bannerObj) => {
  return request.put(`classes/VueBanner/${bannerid}`, bannerObj);
};

// 分类筛选、分页请求方法【可以通过修改参数数据包格式的方式简化代码】
export const prodGet = (
  page = 1,
  category,
  proname,
  price = ["", ""],
  sales = ""
) => {
  let skip = (page - 1) * 10;
  console.log(sales);
  let where = {
    category, //分类严格匹配
    proname: { $regex: `${proname}`, $options: "i" }, //商品名称模糊查询
    originprice: {
      $lte: Number(price[1]),
      $gte: Number(price[0]),
    },
  };
  if (category == "") {
    delete where.category; //删除js对象中的指定字段
  }
  if (!proname) {
    delete where.proname;
  }
  if (!price[0] || !price[1]) {
    delete where.originprice;
  }
  where = JSON.stringify(where); //需要将where处理为json字符串，否则无法成功执行价格范围约束
  let params = {
    limit: 10,
    skip,
    count: 1,
    where,
    order: `${sales == 1 ? "-" : ""}sales`, //销量排序
  };
  if (sales === "") {
    //0==''成立  0===''不成立
    delete params.order;
  }
  return request.get("classes/VueProduct", {
    params,
  });
};

// 加载图表统计数据

export const chartGet = () => {
  return request.get("classes/VueChart");
};
